﻿using System.Collections.Generic; 
using System.Linq; 
using DevExpress.Utils; 
using YDServices;
using YDModel;
using YDCommon;
using System;
using System.Windows.Forms;
using System.Drawing;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;

namespace YDWin.Order
{
    public partial class OrderForm : DevExpress.XtraEditors.XtraForm
    {

        List<V_Order> opList = null;
        V_Order item = null;
        //T_Order_Details item_details = null; 
        OrderService svr = new OrderService();

        public OrderForm()
        {
            InitializeComponent();

            //设置成一次选择一行 
            this.gvEmbryo.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus; 
            this.gvEmbryo.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDown;//单击选择行 不能位click,否则button不能一次点击触发
            this.gvEmbryo.OptionsSelection.EnableAppearanceFocusedCell = false;
            //this.gvEmbryo.OptionsBehavior.Editable = false;//不能被编辑
            //列头禁止移动
            this.gvEmbryo.OptionsCustomization.AllowColumnMoving = false;
            //列头禁止排序
            this.gvEmbryo.OptionsCustomization.AllowSort = false; 
            //隔行换色
            this.gvEmbryo.Appearance.OddRow.BackColor = Color.White;  // 设置奇数行颜色 // 默认也是白色 可以省略 
            this.gvEmbryo.OptionsView.EnableAppearanceOddRow = true;   // 使能 // 和和上面绑定 同时使用有效 
            this.gvEmbryo.Appearance.EvenRow.BackColor = Color.WhiteSmoke; // 设置偶数行颜色 
            this.gvEmbryo.OptionsView.EnableAppearanceEvenRow = true;   // 使能 // 和和上面绑定 同时使用有效
            //禁止使用过滤器
            this.gvEmbryo.OptionsCustomization.AllowFilter = false;
            //禁止显示表头
            this.gvEmbryo.OptionsView.ShowGroupPanel = false;
            //禁止单击列弹出右键菜单
            this.gvEmbryo.OptionsMenu.EnableColumnMenu = false;
            //行高
            this.gvEmbryo.RowHeight= 28;
            //行号列宽度
            this.gvEmbryo.IndicatorWidth = 30;
            //设置横向滚动条
            this.gvEmbryo.OptionsView.ColumnAutoWidth = false;
            this.gvEmbryo.ScrollStyle = ScrollStyleFlags.LiveHorzScroll | ScrollStyleFlags.LiveVertScroll;
            this.gvEmbryo.HorzScrollVisibility = ScrollVisibility.Always;
            //设置表头高
            this.gvEmbryo.ColumnPanelRowHeight = 80;
            //行中值居中
            //this.gvEmbryo.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            //列标题居中
            this.gvEmbryo.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center; 


            //翻页事件
            pageControl1.OnPageChanged += new EventHandler(pagerControl1_OnPageChanged);

            //设置日期控件格式
            this.dtpStartDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpStartDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpStartDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

            this.dtpEndDate.Properties.DisplayFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.EditFormat.FormatString = "yyyy年MM月dd日";
            this.dtpEndDate.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtpEndDate.Properties.Mask.EditMask = "yyyy年MM月dd日";

  
        }
        /// <summary>
        /// 自动行号 自增长
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }


        private void btnQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadDataGrid();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadDataGrid()
        {
            WaitDialogForm sdf = new WaitDialogForm("", "正在查询......");

            item = null;
            ConditionItem[] conds = GetConditions();
            if (conds != null)
            {
                opList = svr.getViewList(conds);
                sdf.Close();
                if (svr.HasError)
                {
                    Utils.ShowPrompt(MessageType.mtWarning, "载入失败\n" + svr.Errors);
                    svr.Errors.Clear();
                }
                else
                {
                    List<V_Order> list = opList.Skip((pageControl1.PageIndex - 1) * pageControl1.PageSize).Take(pageControl1.PageSize).ToList();
                    pageControl1.DrawControl(opList.Count());
                    if (list.Count() == 0)
                    {
                        bsData.DataSource = null;
                    }
                    else
                    {
                        bsData.DataSource = list;
                        this.gvEmbryo.MoveFirst();//移动到第一条记录
                        //UIChecked.BestFit();
                    }
                }
            }
           

        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <returns></returns>
        private ConditionItem[] GetConditions()
        {
            List<ConditionItem> conds = new List<ConditionItem>();

            conds.Add(new ConditionItem
            {
                PropertyName = "Factory_Name",
                FindType = ConditionType.ctLike,
                Values = new object[] { txtFactoryName.Text }
            });

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctGreaterThanOrEqual,
                Values = new object[] { dtpStartDate.EditValue }
            });

            conds.Add(new ConditionItem
            {
                PropertyName = "OperatintTime",
                FindType = ConditionType.ctLessThanOrEqual,
                Values = new object[] { dtpEndDate.EditValue }
            });

            return conds.ToArray();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            LoadDataGrid();
        }
         

        /// <summary>
        /// 删除选中的记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        { 
            //if (item == null) {
            //    Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
            //    return;
            //}
            //DialogResult dr = Utils.ShowPrompt(MessageType.mtInformation, "即将删除该订单信息,删除之后将不可恢复!", null, MessageBoxButtons.OKCancel);
            //if (dr == DialogResult.Cancel)
            //{
            //    return;
            //}
            //svr.DelItem(item); 
            //if (svr.HasError)
            //{
            //    Utils.ShowPrompt(MessageType.mtWarning, "删除失败\n" + svr.Errors);
            //    svr.Errors.Clear();
            //}
            //else
            //{
            //    Utils.ShowPrompt(MessageType.mtInformation, "删除成功\n"); 
            //    LoadDataGrid();
            //}
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //if (item == null)
            //{
            //    Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
            //    return;
            //}
            //AddEmbryoForm form = new AddEmbryoForm();
            //form.Text = "编辑订单信息";
            //form.state = "Edit";
            //form.editTu = item;
            //if (form.ShowDialog() == DialogResult.OK)
            //{
            //    LoadDataGrid();
            //}
        }

        /// <summary>
        /// 点击事件与gvEmbryo.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_Click(object sender, EventArgs e)
        { 
            item = bsData.Current as V_Order; 
        }
        /// <summary>
        /// 鼠标事件 与gvEmbryo.OptionsBehavior.EditorShowMode 设置对应
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_MouseDown(object sender, MouseEventArgs e)
        {
            item = bsData.Current as V_Order;
        }


        /// <summary>
        /// 加载完事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EmbryoForm_Load(object sender, EventArgs e)
        {
            dtpStartDate.EditValue = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);//月初1号
            dtpEndDate.EditValue = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day);//当日+1

        }

        /// <summary>
        /// 胚布采购
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEmbryo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            AddEmbryoForm form = new AddEmbryoForm();
            form.Text = "新增胚布采购信息";
            form.state = "Add";
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

        /// <summary>
        /// 转换显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            //if (e.Column.FieldName.Equals("IsSanding"))
            //{
            //    e.DisplayText = e.Value != null && e.Value.Equals(true) ? "是" : "否";
            //}
        }

        /// <summary>
        /// 无数据显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void gvEmbryo_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            //BindingSource bindingSource = bsData.DataSource as BindingSource;
            //if (bindingSource == null || bindingSource.Count == 0)
            //{
            //    string str = "没有查询到你所想要的数据!";
            //    Font f = new Font("宋体", 10, FontStyle.Bold);
            //    Rectangle r = new Rectangle(e.Bounds.Left + 5, e.Bounds.Top + 5, e.Bounds.Width - 5, e.Bounds.Height - 5);
            //    e.Graphics.DrawString(str, f, Brushes.Black, r);
            //}
        }

        /// <summary>
        /// 磨毛
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSanding_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }

            if (item.SurplusQuantity <= 0 || string.IsNullOrEmpty(item.SurplusQuantity.ToString()))
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            AddSadingForm form = new AddSadingForm();
            form.Text = "新增磨毛信息";
            form.state = "Add";
            form.editTu = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

 
        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
           
        }

        /// <summary>
        /// 印花
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            AddDyeing("新增印花信息");
           
        }

        /// <summary>
        /// 染厂 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void repositoryItemButtonEdit3_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            AddDyeing("新增染厂信息");
        }

        void AddDyeing( string formText)
        {
            if (item == null)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "请选择一条记录!");
                return;
            }

            if (string.IsNullOrEmpty(item.SurplusWeight_Sanding.ToString()) || item.SurplusWeight_Sanding <= 0)
            {
                Utils.ShowPrompt(MessageType.mtWarning, "库存不足!");
                return;
            }

            AddDyeingForm form = new AddDyeingForm();
            form.Text = formText;
            form.state = "Add";
            form.editTu = item;
            if (form.ShowDialog() == DialogResult.OK)
            {
                LoadDataGrid();
            }
        }

    }
}